package com.base.library.utils

import androidx.lifecycle.ViewModelStoreOwner
import java.lang.ref.WeakReference
import java.util.WeakHashMap

/**
 * 共享viewmodel管理类
 */
object ShareViewModelManager {
    private var mSharedViewModelMap = WeakHashMap<String?, ViewModelStoreOwner>()
    fun putSharedViewModel(key: String?, owner: ViewModelStoreOwner) {
        key?.let {
            if (!mSharedViewModelMap.containsKey(it)) {
                mSharedViewModelMap[it] = owner
            }
        }
    }

    fun removeSharedViewModel(key: String?) {
        key?.let {
            mSharedViewModelMap.remove(it)
        }
    }

    fun getShareViewOwner(key: String?): ViewModelStoreOwner? {
        return key?.let {
            mSharedViewModelMap[it]
        }
    }
}